package com.sea.supply.controller;

import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.data.entity.OrderDetail;
import com.sea.data.service.OrderDetailService;
import com.sea.supply.entity.Buy;
import com.sea.supply.entity.Produce;
import com.sea.supply.entity.ProduceDetail;
import com.sea.supply.entity.ProductPrice;
import com.sea.supply.service.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping(value = "/sea/supply/produce")
public class ProduceController {

    @Autowired
    private ProduceService produceService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private ProduceDetailService produceDetailService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ProductPriceService productPriceService;
    /**
     * 管理主页面
     * @param model
     * @return
     */
    @RequestMapping("/main")
    public String main(Model model) {
        //供应商
        model.addAttribute("supplier", supplierService.findByCnd(null));
        return "/sea/supply/produce/main";
    }
    @RequestMapping("/updateDetail")
    public String updateDetail(){
        List<Produce> list = produceService.findByCnd(null);
        for(Produce produce:list){
            produceService.updateProduceTotal(produce.getId());
//            List<ProduceDetail> detailList = produceDetailService.findByProduceId(produce.getId());
//            for(ProduceDetail produceDetail:detailList){
//                Integer detailId = produceDetail.getDetailId();
//                OrderDetail order = orderDetailService.findById(detailId);
//                ProductPrice productPrice = productPriceService.findBySupplierChannelSku(produce.getProduceSupplierName(), order.getOrderChannelName(), order.getOrderSku());
//                produceDetail.setProducePrice(productPrice.getProductPrice());
//                produceDetail.setProduceUnitPrice(produceDetail.getProducePrice()/1.13);
//                produceDetailService.usave(produceDetail);
//            }
        }
        return "success";
     }



    /**
     * 分页条件查询
     *
     * @param pagesize
     * @param start
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByCnd")
    public AjaxTableReturn findByCnd(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                     @RequestParam(value = "offset", defaultValue = "0") int start,
                                     Produce produce) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<Produce> list = produceService.findByCnd(produce);
        PageInfo<Produce> page = new PageInfo<>(list);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }

    /**
     * 修改
     * @param produce
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/usave", method = RequestMethod.POST)
    @RequiresPermissions("produce:check")
    public AjaxReturn usave(Produce produce) {
        AjaxReturn result = Ajax.fail().setMsg("修改失败！");
        try{
            if (null != produce && produceService.usave(produce) > 0) {
                result.setOk(true).setMsg("修改成功！");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result.setMsg(e.getMessage());
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }

    @RequestMapping("/importProduceAgreement")
    @ResponseBody
    public AjaxReturn importFlag(Model model, MultipartFile file) {
        AjaxReturn result = Ajax.fail().setMsg("保存失败！");
        try {
            if (null == file || file.isEmpty()) {
                return result.setMsg("请上传文件！！！");
            } else {
                String fileName = file.getOriginalFilename();
                if (!(fileName.contains(".zip"))) {
                    return result.setMsg("只能上传zip文件！！！");
                }

                produceService.importProduceAgreement(file, fileName);
                result.setOk(true);
                result.setMsg("上传成功！！");
            }
        }catch (RuntimeException e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("系统异常，请联系管理员");
            return result;
        }
        return result;
    }


    /**
     * 修改
     * @param produce
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updatePaymentRemark", method = RequestMethod.POST)
    public AjaxReturn updatePaymentRemark(Produce produce) {
        AjaxReturn result = Ajax.fail().setMsg("修改失败！");
        try{
            if (null != produce && produceService.updatePaymentRemark(produce) > 0) {
                result.setOk(true).setMsg("修改成功！");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result.setMsg(e.getMessage());
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }

    /**
     * 修改
     * @param produce
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/usaveAgreement", method = RequestMethod.POST)
    public AjaxReturn usaveAgreement(Produce produce) {
        AjaxReturn result = Ajax.fail().setMsg("修改失败！");
        try{
            if (null != produce && produceService.usaveAgreement(produce) > 0) {
                result.setOk(true).setMsg("修改成功！");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result.setMsg(e.getMessage());
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/rollback", method = RequestMethod.POST)
    public AjaxReturn rollback(Produce produce) {
        AjaxReturn result = Ajax.fail().setMsg("修改失败！");
        try{
            if (null != produce) {
                produceService.rollBack(produce.getId());
                result.setOk(true).setMsg("修改成功！");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result.setMsg(e.getMessage());
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }
}
